'''''''''
Version='1.1.0 Beta版'
update_date='2020-8-27'

Version='1.1.1  Beta版'
update_date='2021-1-12'
log:
    1.增添了查询姓名数据库获取学号功能
    2.数据库转移到服务器
    
Version='1.1.2  Beta版'
update_date='2021-1-14'
log:
    1.提高登陆速度（改用wait显式登陆）
    
Version='1.1.3  Beta版'
update_date='2021-1-26'
log:
    1.更改Cookie获取方式，提高稳定性
    2.更改post data数据
    3.加入信号。替换login检测
    4.添加登录线程、Cookie刷新线程
'''''''''

from gpadeal import class_data
from PyQt5.QtWidgets import *
from PyQt5.QtCore import *
from PyQt5.QtGui import *
import sys
import pymysql
data_path= './data/'
Version='1.1.4  Beta版'
update_date='2021-2-27'
class School_ui(QMainWindow):
    def __init__(self):
        super().__init__()
        self.setAutoFillBackground(True)
        self.My_ui()
        self.My_Layout()
    def My_ui(self):
        self.Tab=QTabWidget()
        self.tb1=QWidget()
        self.tb2=tb2_ui()
        self.tb3=tb3_ui()
        self.tb4=tb4_ui()
        self.tb5=tb5_ui()
        self.tb6=QWidget()
        self.tb1_ui()
        self.tb5_ui()
        self.Tab.addTab(self.tb1,'使用说明')
        self.Tab.addTab(self.tb2,'登录')
        self.Tab.addTab(self.tb3,'成绩查询')
        self.Tab.addTab(self.tb4,'班级排名')
        self.Tab.addTab(self.tb5,'关于')
        self.Tab.setTabShape(QTabWidget.Triangular)
        self.setFixedSize(900,600)
        self.setWindowTitle('成绩·绩点 查询系统   Beta版')
    def My_Layout(self):
        self.setCentralWidget(self.Tab)
        self.tb3.setEnabled(False)
        self.tb4.setEnabled(False)
        self.tb2.sl.connect(self.enable)
    def tb1_ui(self):
        c1=QPalette()
        label1=QLabel('使用说明')
        label2=QLabel('版本号：{0}\n更新时间：{1}'.format(Version,update_date))
        label3=QLabel('<a href=’*’>*软件所需条件</a>')
        label4=QLabel('<a href=’*’>*使用须知</a>')
        label5=QLabel('(使用功能前请在第二页中刷新Cookie状态)')
        label1.setFont(QFont('kaiti',35))
        c1.setColor(QPalette.Foreground,QColor(207,1,37))
        label1.setPalette(c1)
        label1.setAlignment(Qt.AlignCenter)
        c1.setColor(QPalette.Foreground,QColor(51,51,153))
        label2.setAlignment(Qt.AlignRight)
        label2.setPalette(c1)
        c1.setColor(QPalette.Foreground,QColor(73,189,240))
        for i in [label3,label4]:
            i.setFont(QFont('heiti',20))
            i.setAlignment(Qt.AlignLeft)
            i.setPalette(c1)
            i.linkActivated.connect(self.label_click)
        c1.setColor(QPalette.Foreground,QColor(230,28,98))
        label5.setPalette(c1)
        label5.setFont(QFont('kaiti',22))
        label5.setAlignment(Qt.AlignCenter)
        layout=QVBoxLayout()
        layout.addWidget(label1)
        layout.addWidget(label2)
        layout.addWidget(label3)
        layout.addWidget(label4)
        layout.addWidget(label5)
        self.tb1.setLayout(layout)
    def tb5_ui(self):
        pass
    def label_click(self):
        data=self.sender().text()
        if data=='<a href=’*’>*软件所需条件</a>':
            QMessageBox.information(self, '使用条件','电脑设备为win10，且装有谷歌浏览器（版本需为87.0...），浏览器版本可右击属性查看',QMessageBox.Yes )
        elif data=='<a href=’*’>*使用须知</a>':
            QMessageBox.information(self, '使用须知','本软件制作用于学习交流，不得肆意传播',QMessageBox.Yes )
    def enable(self,bool):
        if bool:
            self.tb3.setEnabled(True)
            self.tb4.setEnabled(True)
        else:
            self.tb3.setEnabled(False)
            self.tb4.setEnabled(False)
class tb2_ui(QWidget):
    sl=pyqtSignal(bool)
    def __init__(self):
        super().__init__()
        self.setIcon()
        self.set_ui()
        self.set_layout()
    def paintEvent(self,event):
        painter=QPainter()
        painter.begin(self)
        painter.setPen(QPen(QColor(140,140,140),5,Qt.DotLine))
        painter.drawLine(600,0,600,600)
        painter.drawLine(600,400,900,400)
        painter.end()
    def set_ui(self):
        p=QPalette()
        self.label1=QLabel('登录:')
        self.label1.setFont(QFont('kaiti',35))
        p.setColor(QPalette.Foreground,QColor(234,97,142))
        self.label1.setPalette(p)
        self.edit1=QLineEdit()
        self.edit1.setFont(QFont('heiti',20))
        self.edit1.setPlaceholderText('请输入你的学号：')
        self.edit2=QLineEdit()
        # self.edit2.setEchoMode(QLineEdit.Password)
        self.edit2.setFont(QFont('heiti',20))
        self.label2=QLabel('Cookie状态:')
        self.label2.setFont(QFont('heiti',15,87))
        p.setColor(QPalette.Foreground,QColor(0,178,188))
        self.label2.setPalette(p)
        self.edit2.setPlaceholderText('请输入你的密码：')
        self.label3=QLabel('请先刷新Cookie信息')
        p.setColor(QPalette.Foreground,QColor(255,235,59))
        self.label3.setPalette(p)
        self.label3.setFont(QFont('heiti',13,87))
        self.btn1=QPushButton('快速登陆')
        self.btn1.setFixedSize(400,50)
        self.btn2=QPushButton('官网登录')
        self.btn2.setFixedSize(200,50)
        self.btn3=QPushButton('登录说明')
        self.btn3.setFixedSize(200,50)
        self.btn4=QPushButton('刷新Cookie状态')
        self.btn4.setFixedSize(200,70)


        self.btn1.clicked.connect(self.btn1_click)
        self.btn2.clicked.connect(self.btn2_click)
        self.btn3.clicked.connect(self.btn3_click)
        self.btn4.clicked.connect(self.btn4_click)

        for i in [self.btn1,self.btn2,self.btn3,self.btn4]:
            op =QGraphicsOpacityEffect()
            op.setOpacity(0.5)
            i.setGraphicsEffect(op)
            i.setAutoFillBackground(True)
        for i in [self.edit1,self.edit2]:
            i.setStyleSheet("background-color:transparent")
    def set_layout(self):
        self.layout=QGridLayout()
        self.layout.addWidget(self.label1,0,0,1,2,Qt.AlignLeft)
        self.layout.addWidget(self.edit1,1,0,1,2,Qt.AlignCenter)
        self.layout.addWidget(self.edit2,2,0,1,2,Qt.AlignCenter)
        self.layout.addWidget(self.label2,0,2,1,1,Qt.AlignCenter)
        self.layout.addWidget(self.label3,1,2,1,1,Qt.AlignCenter)
        self.layout.addWidget(self.btn1,3,0,1,2,Qt.AlignCenter)
        self.layout.addWidget(self.btn2,4,0,1,1,Qt.AlignRight)
        self.layout.addWidget(self.btn3,4,1,1,1,Qt.AlignLeft)
        self.layout.addWidget(self.btn4,4,2,2,1,Qt.AlignCenter)


        self.setLayout(self.layout)
    def setIcon(self):
        palette1 = QPalette()
        # palette1.setColor(self.backgroundRole(), QColor(192,253,123))   # 设置背景颜色
        palette1.setBrush(self.backgroundRole(), QBrush(QPixmap(data_path+'sg.png')))   # 设置背景图片
        self.setPalette(palette1)
        self.setAutoFillBackground(True)
    def btn1_click(self):
        username=self.edit1.text()
        password=self.edit2.text()
        self.td=login_wait((username,password))
        self.td.cl.connect(self.login_signal)
        self.td.start()
        self.btn1.setText('正在登录，请稍等')
        self.btn1.setEnabled(False)
    def btn2_click(self):
        QMessageBox.about(self, '提示','暂未开发，请期待下期版本')
    def btn3_click(self):
        QMessageBox.about(self, '登录说明','软件登录所取的账号密码仅用于获取登录信息的Cookie记录，不会对账号造成任何安全问题···')
    def btn4_click(self):
        self.cw=cookie_wait()
        self.cw.ok.connect(self.cookie_signal)
        self.cw.start()
        self.btn4.setText('正在检测Cookie可用信息...')
        self.btn4.setEnabled(False)
    def login_signal(self,cl):
        self.btn1.setEnabled(True)
        self.btn1.setText('快速登录')
        if cl==0:
            QMessageBox.about(self, '提示','新大官网查询通道正常，欢迎使用！')
            self.sl.emit(True)
        elif cl==1:
            QMessageBox.about(self, '提示','新大官网暂时关闭了查询通道，请稍后再试！')
            self.sl.emit(False)
        elif cl==2:
            QMessageBox.information(self, '提示','账号密码有误，请重新登录',QMessageBox.Yes )
            self.sl.emit(False)
        else:
            QMessageBox.information(self, '提示','网络有异常，请换网重试',QMessageBox.Yes )
            self.sl.emit(False)
    def cookie_signal(self,int):
        self.btn4.setText('刷新Cookie状态')
        if int==0:
            self.sl.emit(True)
            self.btn4.setEnabled(True)
            QMessageBox.about(self, '提示','软件Cookie已刷新，可以使用软件啦')
            QMessageBox.about(self, '提示','新大官网查询通道正常，欢迎使用！')
            self.label3.setText('当前Cookie可用，\n可以使用软件了')
        elif int==1:
            self.sl.emit(False)
            self.btn4.setEnabled(True)
            QMessageBox.about(self, '提示','新大官网暂时关闭了查询通道，请稍后再试！')
            self.label3.setText('当前Cookie可用，\n新大官网暂不可用\n请稍后再试')
        else:
            self.sl.emit(False)
            self.btn4.setEnabled(True)
            QMessageBox.about(self, '提示','当前Cookie不可用，请重新登录')
            self.label3.setText('当前Cookie不可用，\n请重新登录\n刷新获取Cookie')
class tb3_ui(QWidget):
    def __init__(self):
        super().__init__()
        self.setIcon()
        self.set_ui()
        self.set_layout()
        self.connect_SQL()
    def paintEvent(self,event):
        painter=QPainter()
        painter.begin(self)
        painter.setPen(QPen(QColor(140,140,140),5,Qt.DotLine))
        painter.drawLine(600,0,600,600)
        painter.end()
    def set_ui(self):
        p=QPalette()
        self.label1=QLabel('成绩查询：')
        self.label1.setFont(QFont('kaiti',35))
        self.label1.setAlignment(Qt.AlignLeft)
        p.setColor(QPalette.Foreground,QColor(6,214,160))
        self.label1.setPalette(p)

        self.label0=QLabel("姓名：")
        self.label0.setAlignment(Qt.AlignLeft)
        self.label0.setFont(QFont('kaiti',18,80))
        p.setColor(QPalette.Foreground,QColor(81,61,114))
        self.label0.setPalette(p)

        self.label2=QLabel("学号：")
        self.label2.setAlignment(Qt.AlignLeft)
        self.label2.setFont(QFont('kaiti',18,80))
        p.setColor(QPalette.Foreground,QColor(239,71,111))
        self.label2.setPalette(p)

        self.label3=QLabel("学年：")
        self.label3.setAlignment(Qt.AlignLeft)
        self.label3.setFont(QFont('kaiti',18,80))
        p.setColor(QPalette.Foreground,QColor(255,209,102))
        self.label3.setPalette(p)

        self.label4=QLabel("学期：")
        self.label4.setAlignment(Qt.AlignLeft)
        self.label4.setFont(QFont('kaiti',18,80))
        p.setColor(QPalette.Foreground,QColor(38,84,124))
        self.label4.setPalette(p)

        self.label5=QLabel('绩点:')
        self.label5.setFont(QFont('kaiti',15,87))
        p.setColor(QPalette.Foreground,QColor(255,252,249))
        self.label5.setPalette(p)

        self.edit0=QLineEdit()
        self.edit0.setAlignment(Qt.AlignLeft)
        self.edit0.setFont(QFont('heiti',15))
        self.edit0.setFixedSize(150,50)

        self.edit1=QLineEdit()
        self.edit1.setAlignment(Qt.AlignLeft)
        self.edit1.setFont(QFont('heiti',15))

        self.edit2=QLineEdit()
        self.edit2.setText('2020')
        self.edit2.setAlignment(Qt.AlignLeft)
        self.edit2.setFont(QFont('heiti',15))

        self.cb=QComboBox()
        self.cb.setFont(QFont('heiti',15))
        self.cb.addItems(['全学年','第一学期','第二学期'])
        self.cb.setCurrentIndex(1)
        self.btn1=QPushButton('查询')
        self.btn1.setFixedSize(500,80)

        self.btn2=QPushButton('按姓名查询学号')
        self.btn2.setFixedSize(150,50)

        self.list1=QListWidget()
        p.setColor(QPalette.Text,QColor(249,220,92))
        self.list1.setPalette(p)

        self.btn1.clicked.connect(self.btn1_click)
        self.btn2.clicked.connect(self.btn2_click)
        # for i in [self.label0,self.label2,self.label3,self.label1,self.label4,self.edit0,self.edit1,self.edit2,self.btn1,self.list1,self.cb]:
        #     i.setStyleSheet("border:5px solid red")

        for i in [self.edit2,self.edit1,self.list1,self.edit0]:
            i.setStyleSheet("background-color:transparent")

        for i in [self.btn1,self.cb,self.btn2]:
            op =QGraphicsOpacityEffect()
            op.setOpacity(0.5)
            i.setGraphicsEffect(op)
            i.setAutoFillBackground(True)
    def set_layout(self):
        self.layout=QGridLayout()

        self.layout.addWidget(self.label0,1,0,1,1)
        self.layout.addWidget(self.label1,0,0,1,5)
        self.layout.addWidget(self.label2,2,0,1,1)
        self.layout.addWidget(self.label3,3,0,1,1)
        self.layout.addWidget(self.label4,4,0,1,1)

        self.layout.addWidget(self.edit0,1,1,1,1)
        self.layout.addWidget(self.edit1,2,1,1,2)
        self.layout.addWidget(self.edit2,3,1,1,2)

        self.layout.addWidget(self.cb,4,1,1,2)
        self.layout.addWidget(self.btn1,5,0,2,5)
        self.layout.addWidget(self.btn2,1,2,1,1)
        self.layout.addWidget(self.list1,0,4,5,2)
        self.layout.addWidget(self.label5,5,4,1,2)

        self.setLayout(self.layout)

    def setIcon(self):
        palette1 = QPalette()
        # palette1.setColor(self.backgroundRole(), QColor(192,253,123))   # 设置背景颜色
        palette1.setBrush(self.backgroundRole(), QBrush(QPixmap(data_path+'sg.png')))   # 设置背景图片
        self.setPalette(palette1)
        self.setAutoFillBackground(True)

    def btn1_click(self):
            XH=self.edit1.text()
            XN=self.edit2.text()
            XQ=str(self.cb.currentIndex())
            point=[]
            if XQ=='0':
                JD= class_data.score().XN_point(XH, XN)
                self.list1.clear()
                self.list1.addItem('全学年无法查看课程信息\n绩点学分信息已显示')
                self.label5.setText('绩点：{0}\n总学分：{1}'.format(JD[1],JD[0]))
            else:
                data= class_data.score().XQ_point(XH, XN, XQ)
                print(data)
                JD=data[0]
                for i in data[1]:
                    try:
                        point.append('科目：{0}\n学分：{1} 成绩：{2}'.format(i['KCMC'],i['XF'],i['ZCJ']))
                    except:
                        point.append('NULL')
                        self.list1.clear()
                self.label5.setText('绩点：{0}\n总学分：{1}'.format(JD[1],JD[0]))
                self.list1.clear()
                self.list1.addItems(point)

    def btn2_click(self):
            name=self.edit0.text()
            if name=='':
                name='false'
            self.cur.execute("SELECT `学号` FROM `大一`WHERE `姓名`='{0}'".format(name))
            num=self.cur.fetchone()
            if num!=None:
                num=num[0].strip()
                self.edit1.setText(num)
            else:
                QMessageBox.information(self, '提示','没有查询到该同学学号',QMessageBox.Yes )

    def connect_SQL(self):
        try:
            self.ret=pymysql.connect(host='129.211.74.183',port=3306,user='search_user',passwd='123456',db='新大数据')
            self.cur=self.ret.cursor()
        except BaseException as e:
            QMessageBox.information(self,'提示','连接数据库失败，姓名查找功能不可用',QMessageBox.Yes)
            self.btn2.setEnabled(False)
            self.ret=None
            self.cur=None
        else:
            self.btn2.setEnabled(True)
            QMessageBox.information(self,'提示','连接数据库成功！',QMessageBox.Yes)
class tb4_ui(QWidget):
    def __init__(self):
        super().__init__()
        self.XH_list=[]
        self.setIcon()
        self.set_ui()
        self.set_layout()
    def paintEvent(self,event):
        painter=QPainter()
        painter.begin(self)
        painter.setPen(QPen(QColor(140,140,140),5,Qt.DotLine))
        painter.drawLine(600,0,600,600)
        painter.end()
    def set_ui(self):
        p=QPalette()
        self.label1=QLabel('[班级排名]')
        self.label1.setFont(QFont('heiti',15,87))
        p.setColor(QPalette.Foreground,QColor(112,214,255))
        self.label1.setPalette(p)
        self.label2=QLabel('[传入文件]')
        self.label2.setFont(QFont('heiti',15,87))
        p.setColor(QPalette.Foreground,QColor(255,112,166))
        self.label2.setPalette(p)
        self.list1=QListWidget()
        p.setColor(QPalette.Text,QColor(255,214,112))
        self.list1.setPalette(p)
        self.edit=QLineEdit()
        self.edit.setFixedSize(280,30)
        self.file=QPushButton('从文件中导入')
        self.btn=QPushButton('查询')
        self.btn.setEnabled(False)
        self.label3=QLabel("<A href='https://baklibcdn.dagle.cn/t/ab3eed7d-bb55-4fb4-a65b-1b6b24e32c38/u/fb8b5927-4df1-4ee9-85aa-9dd15f7c5a3a/%E5%AF%BC%E5%85%A5%E6%A8%A1%E6%9D%BF15983651789231611644149916.xlsx'>查看格式要求</a>")
        self.label3.setOpenExternalLinks(True)

        self.label3.setFixedSize(280,70)
        self.label3.setFont(QFont('heiti',20,87))
        self.label4=QLabel()
        m=QMovie(data_path+'bn.gif')
        m.start()
        self.label4.setMovie(m)
        self.label4.setFixedSize(280,280)
        self.label5=QLabel('学年：')
        p.setColor(QPalette.Foreground,QColor(239,61,89))
        self.label5.setPalette(p)
        self.label5.setFont(QFont('kaiti',22,80))
        self.label5.setAlignment(Qt.AlignBottom)
        self.edit1=QLineEdit('2020')
        self.edit1.setFont(QFont('heiti',15,80))
        self.edit1.setFixedSize(150,50)
        self.edit1.setAlignment(Qt.AlignTop)
        self.label6=QLabel('学期：')
        p.setColor(QPalette.Foreground,QColor(255,214,112))
        self.label6.setPalette(p)
        self.label6.setFont(QFont('kaiti',22,80))
        self.label6.setAlignment(Qt.AlignBottom)
        self.cb=QComboBox()
        self.cb.addItems(['全学期','第一学期','第二学期'])
        self.cb.setCurrentIndex(1)
        self.cb.setFont(QFont('kaiti',15))

        self.file.clicked.connect(self.file_click)
        self.btn.clicked.connect(self.btn_click)

        for i in [self.edit,self.list1,self.edit1]:
            i.setStyleSheet("background-color:transparent")
        for i in[self.btn,self.file,self.cb,self.label4]:
            op =QGraphicsOpacityEffect()
            if i==self.label4:
                op.setOpacity(0.8)
            else:
                op.setOpacity(0.5)
            i.setGraphicsEffect(op)
            i.setAutoFillBackground(True)
    def set_layout(self):
        self.layout=QHBoxLayout()
        layout1=QVBoxLayout()
        layout2=QVBoxLayout()
        layout3=QVBoxLayout()
        layout1.addWidget(self.label1)
        layout1.addWidget(self.list1)
        layout3.addWidget(self.label2)
        layout3.addWidget(self.edit)
        layout3.addWidget(self.file)
        layout3.addWidget(self.btn)
        layout3.addWidget(self.label3)
        layout3.addWidget(self.label4)
        layout2.addWidget(self.label5)
        layout2.addWidget(self.edit1)
        layout2.addWidget(self.label6)
        layout2.addWidget(self.cb)
        self.layout.addLayout(layout1)
        self.layout.addLayout(layout2)
        self.layout.addLayout(layout3)
        self.setLayout(self.layout)
    def setIcon(self):
        palette1 = QPalette()
        # palette1.setColor(self.backgroundRole(), QColor(192,253,123))   # 设置背景颜色
        palette1.setBrush(self.backgroundRole(), QBrush(QPixmap(data_path+'sg.png')))   # 设置背景图片
        self.setPalette(palette1)
        self.setAutoFillBackground(True)
    def file_click(self):
            self.list1.clear()
            route=QFileDialog.getOpenFileName(self,'Open file','C:\\','xlsx xls files (*.xlsx *.xls)')[0]
            print(route)
            try:
                self.XH_list= class_data.score().import_data(route)
                self.list1.addItems(self.XH_list)
                self.edit.setText(route)
                self.btn.setEnabled(True)
            except:
                QMessageBox.information(self, '提示','查询的文件格式错误或为空',QMessageBox.Yes )
                self.btn.setEnabled(False)
    def btn_click(self):
        XN=self.edit1.text()
        XQ=str(self.cb.currentIndex())
        QMessageBox.information(self, '提示','本次查询'+str(len(self.XH_list))+'条数据，预计不会超过4分钟，请耐心等待，不要关闭软件。',QMessageBox.Yes )
        data_list= class_data.score().sort_data(self.XH_list, XN, XQ)
        self.list1.clear()
        self.list1.addItems(data_list)
class tb5_ui(QWidget):
    def __init__(self):
        super().__init__()
    def paintEvent(self,event):
        painter=QPainter()
        painter.begin(self)
        painter.setPen(QPen(QColor(140,140,140),5,Qt.DotLine))
        painter.drawLine(600,0,600,600)
        painter.end()
class login_wait(QThread):
    cl=pyqtSignal(int)
    def __init__(self,data):
        super().__init__()
        self.data=data
    def run(self):
        a= class_data.score()
        b=a.save_cookie(self.data)
        if b==1:
            c= class_data.score().text_web()
            if c:
                self.cl.emit(0)
            else:
                self.cl.emit(1)
        elif b==0:
            self.cl.emit(2)
        else:
            self.cl.emit(3)
class cookie_wait(QThread):
    ok=pyqtSignal(int)
    def __init__(self):
        super().__init__()
    def run(self):
        a= class_data.score().text_cookie()
        if a==1:
            b= class_data.score().text_web()
            if b:
                self.ok.emit(0)
            else:
                self.ok.emit(1)
        else:
            self.ok.emit(2)
if __name__=='__main__':
    app=QApplication(sys.argv)
    fram=School_ui()
    fram.show()
    sys.exit(app.exec_())
